//
//  PicDetailVC.m
//  OpenEyes
//
//  Created by dllo on 15/9/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PicDetailVC.h"
#import "PicDetailModel.h"
#import "PicDetailTVCell.h"
#import "LastVC.h"
#import "MJRefresh.h"
@interface PicDetailVC ()<UITableViewDataSource,UITableViewDelegate,requestDelegate>

/**
 *  详情tableView
 */
@property (nonatomic, strong) UITableView *tableView;


/**
 *  数组
 */
@property (nonatomic, strong) NSMutableArray *array;



@end

@implementation PicDetailVC



- (void)dealloc {

    [HttpRequest httpRequestHandle].delegate = nil;

    }
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}


-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    self.navigationItem.title = self.getCategory;

    [self GetDate];
    [self  creatTableView];
    self.count =1;
    
 
    
    // Do any additional setup after loading the view.
}



-(void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT + 44) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.tableView];
    
 //刷新
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(action)];
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(up)];
 
// 菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.removeFromSuperViewOnHide = YES;
    self.HUD.labelText = @"loading.....";
    [self.view addSubview:self.HUD];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    
   


    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fanhui-3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(left)];
    
}

-(void)up{
    [self.tableView reloadData];
    [self GetDate];
    [self.tableView.header endRefreshing];
    
}
-(void)action{
    [self.tableView reloadData];
    [self GetDate];
    [self.tableView.footer endRefreshing];
}
-(void)left{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)GetDate{
    self.count ++;
    NSString *url = @"token=ARk6MCqumCVjvSM1yEuacwNu549OA7hujzU%3D";
    [[HttpRequest httpRequestHandle]loadRequestWithUrl:@"http://lab.daigj.com/api/ios/hm/list" method:@"POST" pramar:[NSString stringWithFormat:@"version=1.0.3&pageNo=%ld&pageSize=10&%@&category=%@",self.count, url,self.getCategory]];
    [HttpRequest httpRequestHandle].delegate = self;


}
-(void)requestData:(id)data
{
//    NSLog(@"%@",data);
    NSMutableArray *listArray = data [@"data"] [@"data"] [@"list"];
    for (NSDictionary *dic in listArray) {
        PicDetailModel *pic = [[PicDetailModel alloc]init];
        [pic setValuesForKeysWithDictionary:dic];
        pic.cid = [dic objectForKey:@"id"];
        [self.array addObject:pic];
     
    }
    [self.HUD setHidden:YES];
    [self.tableView reloadData];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT / 4 * 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reused = @"cell";
    PicDetailTVCell *cell = [tableView dequeueReusableCellWithIdentifier:reused];
    if (cell == nil) {
        cell = [[PicDetailTVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reused];
    }
    
    PicDetailModel *model = [self.array objectAtIndex:indexPath.row];
    [cell getVuale:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LastVC *last = [[LastVC alloc]init];
    [self.navigationController pushViewController:last animated:YES];
    
    PicDetailModel *model = self.array[indexPath.row];
    last.getId = model.cid;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
